package com.example.eldersystem.operatedata.service;

import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.mapper.VolunteerMapper;
import com.example.eldersystem.operatedata.model.Volunteer;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class VolunteerService {

    private VolunteerMapper volunteerMapper;

    @Autowired
    public void setVolunteerMapper (VolunteerMapper volunteerMapper) {
        this.volunteerMapper = volunteerMapper;
    }

    public int getVolunteerNum(){
        return volunteerMapper.selectVolunteerNum();
    }

    public DataResponse<String> addVolunteer(Volunteer volunteer){
        DataResponse<String> dataResponse = new DataResponse<>();
        if (volunteerMapper.addVolunteer(volunteer)>0){
            dataResponse.setMsg("添加义工成功");
        }else {
            dataResponse.setCode(111);
            dataResponse.setMsg("添加义工失败");
        }
        return dataResponse;
    }

    public DataResponse<List<Volunteer>> selectVolunteer(int from, int num){
        DataResponse<List<Volunteer>> dataResponse = new DataResponse<>();
        List<Volunteer> list;
        int total = volunteerMapper.selectVolunteerNum();
        if(total<=0){
            dataResponse.setCode(10);
            dataResponse.setMsg("查询无结果");
        }else{
            dataResponse.setMsg("查询成功");
            list = volunteerMapper.selectVolunteer(from,num);
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<List<Volunteer>> selectVolunteerByName(String name){
        DataResponse<List<Volunteer>> dataResponse = new DataResponse<>();
        List<Volunteer> list;
        int total = volunteerMapper.selectVolunteerNum2(name);
        if(total<=0){
            dataResponse.setCode(10);
            dataResponse.setMsg("查询无结果");
        }else{
            dataResponse.setMsg("查询成功");
            list = volunteerMapper.selectVolunteerByName(name);
            dataResponse.setData(list);
        }
        return dataResponse;
    }

    public DataResponse<String> updateVolunteer(Volunteer volunteer){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = volunteerMapper.updateVolunteer(volunteer);
        if(num==0){
            dataResponse.setCode(131);
            dataResponse.setMsg("更新失败，查询不到对应的义工");
        }else{
            dataResponse.setMsg("更新成功");
        }
        return dataResponse;
    }

    public DataResponse<String> updateProfile(int id,String profilePhoto){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = volunteerMapper.updateProfile(id,profilePhoto);
        dataResponse.setCode(0);
        if(num==0){
            dataResponse.setCode(131);
            dataResponse.setMsg("更新失败，查询不到对应的义工");
        }else{
            dataResponse.setMsg("更新照片成功");
        }
        return dataResponse;
    }


    public DataResponse<String> deleteVolunteer(int id){
        DataResponse<String> dataResponse = new DataResponse<>();
        int num = volunteerMapper.deleteVolunteer(id);
        if(num==0){
            dataResponse.setCode(121);
            dataResponse.setMsg("删除失败");
        }else{
            dataResponse.setMsg("删除成功");
        }
        return dataResponse;
    }
}
